package com.runen.infinitemovement.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.runen.infinitemovement.R;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;
//import com.umeng.socialize.ShareAction;
//import com.umeng.socialize.UMShareListener;
//import com.umeng.socialize.bean.SHARE_MEDIA;
//import com.umeng.socialize.media.UMImage;
//import com.umeng.socialize.media.UMWeb;

public class CommonDetailsPoppupWindow extends PopupWindow {
    private ImageView iv_dismiss;
    private TextView tv_title_name, tv_details, tv_sure, tv_cancel;
    private LinearLayout ll_bottom_text;
    private Context context;
    private View layout;
    private String name;
    private String introduction;
    private int flag;

    public OnClickListenr getOnClickListenr() {
        return onClickListenr;
    }

    public void setOnClickListenr(OnClickListenr onClickListenr) {
        this.onClickListenr = onClickListenr;
    }

    private OnClickListenr onClickListenr;

    public static CommonDetailsPoppupWindow getInstance(Context context, String name, String introduction, int flag) {
        return new CommonDetailsPoppupWindow(context, name, introduction, flag);
    }

    private CommonDetailsPoppupWindow(Context context, String name, String introduction, int flag) {
        super(context);
        this.context = context;
        this.name = name;
        this.introduction = introduction;
        this.flag = flag;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }


    private void initView() {
        layout = View.inflate(context, R.layout.common_details_popup_window, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        tv_title_name = layout.findViewById(R.id.tv_title_name);
        tv_details = layout.findViewById(R.id.tv_details);
        tv_cancel = layout.findViewById(R.id.tv_cancel);
        tv_sure = layout.findViewById(R.id.tv_sure);
        LinearLayout ll_bottom_text = layout.findViewById(R.id.ll_bottom_text);
        tv_title_name.setText(name);
        tv_details.setText(introduction);
        if (flag == 1) {
            tv_title_name.setVisibility(View.GONE);
            ll_bottom_text.setVisibility(View.VISIBLE);
        } else {
            tv_title_name.setVisibility(View.VISIBLE);
            ll_bottom_text.setVisibility(View.GONE);
        }
        if (introduction.contains("请勿重复挑战")) {
            ll_bottom_text.setVisibility(View.GONE);
        }

    }

    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenr != null) {
                    onClickListenr.onSureClick();
                    dismiss();
                }
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenr != null) {
                    onClickListenr.onCancelClick();
                    dismiss();
                }
            }
        });
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.4f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnClickListenr {
        void onCancelClick();

        void onSureClick();
    }
}
